package com.question.java.time.exec04;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Calendar;

/**
 * @Description: 将java.util.Calendar转换成java.time.ZonedDateTime类
 * @Author: Jun Yang
 */
public class DateTimeExample23 {

    // 考虑时区
    public static ZonedDateTime convertFromCalendar(Calendar cal) {
        return ZonedDateTime.ofInstant(cal.toInstant(), cal.getTimeZone().toZoneId());
    }

    // 不考虑时区，利用get方法将java.util.Calendar转换成java.time.LocalDateTime
    public static LocalDateTime convertFromCalendarUsingGetter(Calendar cal) {
        return LocalDateTime.of(cal.get(Calendar.YEAR),
                cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH),
                cal.get(Calendar.HOUR),
                cal.get(Calendar.MINUTE),
                cal.get(Calendar.SECOND));
    }
}
